new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQLSerialNumber: "P_business_sales_order_detail2",
                Condition: '',
                ExtensionCondition: '',
                Supplier_SerialNumber: [],
                Sales_Order_SerialNumber: '',
                Sales_Date: null,
                limit: 20,
                page: 1,
            },
            rules: {
                Supplier_SerialNumber: [{ required: true, message: '请选择供应商', trigger: 'change' }],
            },
            Supplier: [],
            tableData: [],	//表格数据
            loading: false,
        }
    },
    mounted() {
        this.getData();
    },
    computed: {
        Type() {
            return function (str) {
                switch (str) {
                    case "0":
                        return "待发货";
                    case "1":
                        return "发货中";
                    case "2":
                        return "已发货";
                    default:
                        return ""
                }
            }
        }
    },
    methods: {
        onSerach(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.form.PaginationNumber = 1;
                    this.getList(this.form)
                }else {
                    this.$message({
                        message: '请选择供应商',
                        type: 'warning'
                    })
                    return false;
                }
            })
        },
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
        },
        getData() {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_supplier_info',
                    Condition: "Row_Status <> '1'",
                },
                success: function (res) {
                    that.Supplier = res;
                },
                error: function () {
                    that.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        submitForm() {
            const that = this
            if (this.tableData.length > 0) {
                let arr = []
                this.tableData.forEach(item => {
                    //修改状态
                    let Reconciliation_Status = item.Reconciliation_Status.charAt(1);
                    item.Reconciliation_Status = '1' + Reconciliation_Status;
                    let is = true;
                    arr.forEach(obj => {
                        if (obj.Supplier_SerialNumber == item.Supplier_SerialNumber) {
                            obj.Purchase_Amount = accAdd(obj.Purchase_Amount, accMul(item.Delivery_Quantity, item.Purchase_Unit_Price))
                            obj.Children.business_purchase_order_detail.push({
                                Purchase_Order_Detail_SerialNumber: '',
                                Purchase_Order_SerialNumber: '',
                                Unit_Price_SerialNumber: item.Execute_Unit_Price_SerialNumber,
                                Sales_Order_Detail_SerialNumber: item.Sales_Order_Detail_SerialNumber,
                                Supplier_Article_SerialNumber: item.Supplier_Article_SerialNumber,
                                Purchase_Unit_Price: item.Purchase_Unit_Price,
                                Quantity: item.Delivery_Quantity,
                                Receipt_Quantity: 0,
                                Not_Receipt_Quantity: 0,
                                Shippment_Quantity: 0,
                                Inventory_Quantity: 0,
                                Amount: accMul(item.Delivery_Quantity, item.Purchase_Unit_Price),
                            })
                            is = false;
                        }
                    })
                    if (is) {//新
                        arr.push({
                            Purchase_Order_SerialNumber: '',
                            Supplier_SerialNumber: item.Supplier_SerialNumber,
                            Purchase_Date: getYMDHMSDate(),
                            Transaction_Type: '',
                            Purchase_Amount: accMul(item.Delivery_Quantity, item.Purchase_Unit_Price),
                            Paid_Amount: 0,
                            Apply_Amount: 0,
                            Confirm_Amount: 0,
                            Row_Status:'00',
                            Children: {
                                business_purchase_order_detail: [{
                                    Purchase_Order_Detail_SerialNumber: '',
                                    Purchase_Order_SerialNumber: '',
                                    Unit_Price_SerialNumber: item.Execute_Unit_Price_SerialNumber,
                                    Sales_Order_Detail_SerialNumber: item.Sales_Order_Detail_SerialNumber,
                                    Supplier_Article_SerialNumber: item.Supplier_Article_SerialNumber,
                                    Purchase_Unit_Price: item.Purchase_Unit_Price,
                                    Quantity: item.Delivery_Quantity,
                                    Receipt_Quantity: 0,
                                    Not_Receipt_Quantity: 0,
                                    Shippment_Quantity: 0,
                                    Inventory_Quantity: 0,
                                    Amount: accMul(item.Delivery_Quantity, item.Purchase_Unit_Price),
                                }]
                            }
                        })
                    }
                });
                $.ajax({
                    headers:requestHeaders(),
                    url: `${BASE_URL2}/generalBusiness/generalSave`,
                    type: 'POST',
                    data: {
                        Data: JSON.stringify({ 
                            Add: { business_purchase_order: arr },
                            Modify: { business_sales_order_detail:that.tableData }
                        })
                    },
                    success: function (res) {
                        if (res.Status === 'success') {
                            that.$message.success("提交成功")
                            window.history.go(-1)
                        } else {
                            that.$message.error("提交失败，请稍后再试")
                        }
                    },
                    complete: function (XMLHttpRequest) {
                        that.loading = false
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json"
                })
            } else {
                that.$message({
                    message: '当前没有需要对账的订单明细',
                    type: 'warning'
                })
            }
        },
        getList(form) {//换页方法
            const that = this
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%")))
            let sql = "";
            if (data.Sales_Date) {
                sql = sql + "AND business_collection_detail.Collection_Date BETWEEN '" + data.Sales_Date[0] + "' AND '" + data.Sales_Date[1] + "'\r\n"
            }
            if (data.Supplier_SerialNumber.length > 0) {
                let str = "";
                data.Supplier_SerialNumber.forEach(item => {
                    if (str) {//后续
                        str = str  + " OR business_sales_order_detail.Supplier_SerialNumber = '" + item + "'\r\n";
                    }else {//首次
                        str = " business_sales_order_detail.Supplier_SerialNumber = '" + item + "'\r\n";
                    }
                })
                sql = sql + "AND ("+ str +") \r\n"
            }
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumberWithOriginal`,
                type: 'POST',
                data,
                success: function (Result) {
                    console.log(Result);
                    that.tableData = Result;
                    if (Result.length == 0) {
                        that.$message({
                            message: '未查询到可对账的订单明细',
                            type: 'warning'
                        })
                    }
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        handleRemove(index) {
            this.$confirm('是否要删除此行?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = this.tableData.filter((i, id) => id != (this.form.page - 1) * this.form.limit + index);
            }).catch(() => { });
        }
    },
})